package com.yhty.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yhty.utils.Arith;

import com.yhty.dao.OweDao;
import com.yhty.dao.WeDefineDao;
import com.yhty.dao.WeMonthDao;
import com.yhty.model.Owe;
import com.yhty.model.OweList;
import com.yhty.model.WeDefine;
import com.yhty.model.WeList;
import com.yhty.model.WeMonth;
import com.yhty.service.WeManager;
import com.yhty.utils.DateUtil;

/**
 * 水电业务逻辑实现类
 * @author RoySong
 * 2011-3-31-下午09:59:16
 */
@Service("weManager")
@Transactional
public class WeManagerImpl implements WeManager{
	private WeDefineDao weDefineDao;
	private WeMonthDao weMonthDao;
	private OweDao oweDao;
	
	/*
	 * 初始化当月水电费用信息
	 * @see com.yhty.service.WeManager#init()
	 */
	public Void init() {
		//如果本月已初始化,则不生成水电费用信息
		if(weMonthDao.isWeInit())
			return null;
		List<WeMonth> wmList = weMonthDao.getMonth();
		WeDefine wd = weDefineDao.get();
		for(WeMonth wm:wmList){
			wm.setW_price(wd.getWater_price());
			wm.setE_price(wd.getElectric_price());
		}
		weMonthDao.batchAdd(wmList);
		weMonthDao.init();
		return null;
	}

	@Resource(name="weMonthDao")
	public void setWeMonthDao(WeMonthDao weMonthDao) {
		this.weMonthDao = weMonthDao;
	}

	@Resource(name="weDefineDao")
	public void setWeDefineDao(WeDefineDao weDefineDao) {
		this.weDefineDao = weDefineDao;
	}

	/**
	 * 获取本月水电费用信息列表
	 */
	public List<WeMonth> getMonth() {
		return weMonthDao.getMonth(DateUtil.getToday());
	}

	@Resource(name="oweDao")
	public void setOweDao(OweDao oweDao) {
		this.oweDao = oweDao;
	}

	/*
	 * 按月获取水电详情列表
	 * @see com.yhty.service.WeManager#getByDate(java.lang.String)
	 */
	public List<WeList> getByDate(String date) {
		List<OweList> oList=oweDao.getOweListByMonth(date);
		List<WeMonth> wList=weMonthDao.getMonth(date);
		List<WeList> weList = new ArrayList<WeList>();
		for(OweList o:oList)
			for(WeMonth w:wList){
				if(o.getRent_id().longValue()==w.getRent_id().longValue()){
					WeList wl = new WeList();
					wl.setOweList(o);
					wl.setWeMonth(w);
					weList.add(wl);
					break;
				}
			}
		return weList;
	}

	/*
	 * 修改所有水电单价信息
	 * @see com.yhty.service.WeManager#updateDefine(com.yhty.model.WeDefine)
	 */
	public void updateDefine(WeDefine wd) {
		//更新水电单价
		weDefineDao.update(wd);
		//批量更新本月水电单价信息
		List<WeMonth> wList = weMonthDao.getMonth(DateUtil.getToday());
		Map<Long,Float[]> num = new HashMap<Long,Float[]>(); 
		for(WeMonth wm:wList){
			//记录水电量
			Float[] weNum = new Float[2];
			weNum[0] = wm.getW_num();
			weNum[1] = wm.getE_num();
			num.put(wm.getRent_id(), weNum);
			//更新本月水电单价
			wm.setW_price(wd.getWater_price());
			wm.setE_price(wd.getElectric_price());
		}
		weMonthDao.batchUpdate(wList);
		//批量更新本月欠费信息
		List<Owe> oList = oweDao.getByMonth(DateUtil.getToday());
		for(Owe o:oList){
			WeMonth wm = new WeMonth();
			wm.setE_price(wd.getElectric_price());
			wm.setW_price(wd.getWater_price());
			wm.setE_num(num.get(o.getRent_id())[1]);
			wm.setW_num(num.get(o.getRent_id())[0]);
			changeOweByWe(o, wm);
		}
		oweDao.batchUpdateWe(oList);
	}

	/*
	 * 修改某条水电信息
	 * @see com.yhty.service.WeManager#updateWeMonth(com.yhty.model.WeMonth)
	 */
	public void updateWeMonth(WeMonth wm) {
		weMonthDao.update(wm);
		Owe o = oweDao.getByRentId(DateUtil.getToday(), wm.getRent_id());
		if(o != null){
			changeOweByWe(o, wm);
			oweDao.update(o);
		}
	}

	/**
	 * 根据水电单价记录修改欠费记录
	 * @param o
	 * @param wm
	 */
	private void changeOweByWe(Owe o,WeMonth wm){
		//总欠费记录减去原水电欠费记录
		float all = Arith.sub(o.getAll(), o.getOwe().getWater());
		all = Arith.sub(all, o.getOwe().getElectric());
		//更新水电欠费记录
		o.getOwe().setWater(Arith.mul(wm.getW_num(), wm.getW_price()));
		o.getOwe().setElectric(Arith.mul(wm.getE_num(), wm.getE_price()));
		//总欠费记录加上更新后的水电欠费记录
		all = Arith.add(all, o.getOwe().getWater());
		all = Arith.add(all, o.getOwe().getElectric());
		o.setAll(all);
	}

	/*
	 * 根据租房关系和日期获取水电信息
	 * @see com.yhty.service.WeManager#getByRentId(java.lang.Long, java.lang.String)
	 */
	public WeMonth getByRentId(Long rentId, String today) {
		WeMonth we = null;
		try{ 
			we = weMonthDao.getByRentId(rentId,today);
		}catch(EmptyResultDataAccessException e){
			return null;
		}
		return we;
	}
}
